package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public void add(SetmealDTO setMealDTO) {
        // 1.保存套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setMealDTO, setmeal);
        setmealMapper.insert(setmeal);

        List<SetmealDish> setmealDishes = setMealDTO.getSetmealDishes();
        // 2.保存套餐和菜品的关系
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        });
    }

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {

        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.selectByQuery(setmealPageQueryDTO.getName(),setmealPageQueryDTO.getCategoryId(),setmealPageQueryDTO.getStatus());
        List<SetmealVO> result = page.getResult();
        // 填充分类名称 1.遍历 2.根据分类id查询分类名称
        result.forEach(SetmealVO -> {
            String name = categoryMapper.getNameById(SetmealVO.getCategoryId());
            SetmealVO.setCategoryName(name);
        });
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public SetmealDTO getById(Integer id) {
        // 1.根据id查询套餐
        SetmealDTO setmealDTO = setmealMapper.getById(id);
        // 2.根据套餐id查询菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        setmealDTO.setSetmealDishes(setmealDishes);
        return setmealDTO;
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        // 1.更新套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);
        // 2.删除套餐和菜品的关系
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        // 3.保存套餐和菜品的关系
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        });
    }

    @Override
    public void delete(Integer[] ids) {
        // 1.判断套餐是否启用
        List<Setmeal> list = setmealMapper.getByIds(ids);

        list.forEach(setmeal -> {
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException("套餐启用中，无法删除");
            }
        });
        // 2.删除套餐和菜品的关系
        setmealMapper.deleteByIds(ids);
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        // 1.判断套餐是否启用
        if (status.equals(StatusConstant.ENABLE)) {
            List<SetmealDish> list = setmealDishMapper.getBySetmealId(id);
            ArrayList<Long> list1 = new ArrayList<>();
            list.forEach(setmealDish -> {
                list1.add(setmealDish.getDishId());
            });

            list1.forEach(aLong -> {
                Dish dish = dishMapper.getByIdLong(aLong);
                if (dish.getStatus().equals(StatusConstant.DISABLE)) {
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        // 2.更新套餐状态
        Setmeal setmeal = new Setmeal();
        setmeal.setId((long)id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    @Override
    public List<Setmeal> getByCategroyId(Integer categoryId) {
        List<Setmeal> list = setmealMapper.getByCategroyId(categoryId);
        return list;
    }

    @Override
    public List<Dish> getDishBySetmealId(Integer id) {
        List<Dish> list = setmealMapper.getDishBySetmealId(id);
        return list;
    }
}
